from django.shortcuts import render, redirect
from .models import Student, Score
from django.contrib.auth import authenticate, login, logout
from django.core.exceptions import ObjectDoesNotExist


# Create your views here.


def index(request):
    item = {}
    if request.method == 'POST':
        if request.POST['postsearch'] == '':
            content = {'warning': '请输入搜索内容'}
            return render(request, 'student/index.html', content)
        else:
            key = -1
            for person in Student.objects.all():

                if str(person.Code) == request.POST['postsearch']:
                    key = person.id
                    item = person
                    content = {'item': item}
                    return render(request, 'student/index.html', content)
                elif person.Name == request.POST['postsearch']:
                    key = person.id
                    item = person
                    content = {'item': item}
                    return render(request, 'student/index.html', content)

            if key == -1:
                content = {'none': request.POST['postsearch']+' 该姓名or单号不存在'}
                return render(request, 'student/index.html', content)

    else:
        content = {'none': '通知：一轮面试将于9月27号晚进行'}
        return render(request, 'student/index.html',content)


def score(request):
    if request.user.is_authenticated:
        content = {'person': Student.objects.all()}
        return render(request, 'student/score.html', content)
    else:

        return redirect('student:Index')


def manager(request):
    if request.user.is_authenticated:
        content = {'person': Student.objects.all()}
        return render(request, 'student/manager.html', content)
    else:
        return redirect('student:Index')


def logins(request):
    if request.method == 'POST':
        user = authenticate(request, username=request.POST['username'], password=request.POST['password'])
        if user is None:
            return render(request, 'student/login.html', context={'warning': 'warning'})
        else:
            login(request, user)
            return redirect('student:Index')
    else:

        return render(request, 'student/login.html')


def logouts(request):
    logout(request)
    return redirect('student:Index')


def about(request):
    date = [
        {'day':20180823,'content':'新建页 提交可选项时抛出UnboundLocalError异常'},
        {'day':20180823,'content':'列出数据表时对未建的表读取抛出ObjectDoesNotExist异常'}
    ]
    content = {'date':date}
    return render(request, 'student/about.html',content)


def addnew(request):
    global birthday
    global selfs
    if request.method == 'POST':

        if request.POST['Name'] != '':
            name = request.POST['Name']
        else:
            content = {'warning': '请输入姓名'}
            return render(request, 'student/addnew.html', content)

        if request.POST['Code'] != '':
            code = int(request.POST['Code'])
        else:
            content = {'warning': '请输入学号'}
            return render(request, 'student/addnew.html', content)

        if request.POST['College'] != '未选':
            college = request.POST['College']
        else:
            content = {'warning': '请选择学院'}
            return render(request, 'student/addnew.html', content)

        if request.POST['Class'] != '':
            classes = request.POST['Class']
        else:
            content = {'warning': '请输入班级'}
            return render(request, 'student/addnew.html', content)

        if request.POST['QQ'] != '':
            qq = int(request.POST['QQ'])
        else:
            content = {'warning': '请输入QQ号'}
            return render(request, 'student/addnew.html', content)

        if request.POST['Tel'] != '':
            tel = int(request.POST['Tel'])
        else:
            content = {'warning': '请输入电话号码'}
            return render(request, 'student/addnew.html', content)

        selfs = ''
        if request.POST['Self'] != '':
            selfs = request.POST['Self']

        birthday = ''
        if request.POST['Birthday'] != '':
            birthday = request.POST['Birthday']
            item = Student(Name=name, Code=code, College=college, Class=classes, QQ=qq, Tel=tel, Self=selfs, Birthday=birthday)
            item.save()
        else:
            item = Student(Name=name, Code=code, College=college, Class=classes, QQ=qq, Tel=tel, Self=selfs)
            item.save()
        content = {'warning': '添加成功'}
        return render(request, 'student/addnew.html', content)
    else:
        return render(request, 'student/addnew.html')


def mark(request, item_id):
    if request.method == 'POST':
        itemid = int(request.POST['itemid'])
        item = Student.objects.get(id=itemid)

        # newscore = Score.objects.get(id=itemid)
        # newscore.Names=item
        # newscore.Pentest=pentest
        # newscore.Outer=outer
        # newscore.Inner=inner
        # newscore.Interest=interest
        # newscore.Want=want
        # newscore.Sum=sum
        # newscore.save()

        try:
            scores = Score.objects.get(id=itemid)
            if int(request.POST['Pentest']) != 0:
                pentest = int(request.POST['Pentest'])
            else:
                pentest = int(scores.Pentest)

            if int(request.POST['Outer']) != 0:
                outer = int(request.POST['Outer'])
            else:
                outer = int(scores.Outer)

            if int(request.POST['Inner']) != 0:
                inner = int(request.POST['Inner'])
            else:
                inner = int(scores.Inner)

            if int(request.POST['Interest']) != 0:
                interest = int(request.POST['Interest'])
            else:
                interest = int(scores.Interest)

            if int(request.POST['Want']) != 0:
                want = int(request.POST['Want'])
            else:
                want = int(scores.Want)

            scores.Pentest = pentest
            scores.Outer = outer
            scores.Inner = inner
            scores.Interest = interest
            scores.Want = want
            sum = pentest + outer + inner + interest + want
            scores.Sum = sum
            scores.save()
        except ObjectDoesNotExist:
            pentest = int(request.POST['Pentest'])
            outer = int(request.POST['Outer'])
            inner = int(request.POST['Inner'])
            interest = int(request.POST['Interest'])
            want = int(request.POST['Want'])
            sum = pentest + outer + inner + interest + want
            scores = Score(Names=item, Pentest=pentest, Outer=outer, Inner=inner, Interest=interest, Want=want, Sum=sum)
            scores.save()
        content = {'item': item, 'score': scores}
        return render(request, 'student/mark.html', content)

    else:
        item = Student.objects.get(id=item_id)
        try:
            scores = Score.objects.get(id=item_id)
        except ObjectDoesNotExist:
            scores = {}
        content = {'item': item, 'score': scores}
        return render(request, 'student/mark.html', content)


def change(request, item_id):
    if request.method == 'POST':
        itemid = int(request.POST['id'])
        item = Student.objects.get(id=itemid)
        if request.POST['Name'] != '':
            item.Name = request.POST['Name']

        if request.POST['Code'] != '':
            item.Code = int(request.POST['Code'])

        if request.POST['College'] != '未选':
            item.College = request.POST['College']

        if request.POST['Class'] != '':
            item.Class = request.POST['Class']

        if request.POST['QQ'] != '':
            item.QQ = int(request.POST['QQ'])

        if request.POST['Tel'] != '':
            item.Tel = int(request.POST['Tel'])

        item.save()
        item = Student.objects.get(id=itemid)
        try:
            scores = Score.objects.get(id=itemid)
        except ObjectDoesNotExist:
            scores = ''
        content = {'item': item, 'score': scores}
        return render(request, 'student/changeinfo.html', content)


    else:
        item = Student.objects.get(id=item_id)
        try:
            scores = Score.objects.get(id=item_id)
        except ObjectDoesNotExist:
            scores = {}
        content = {'item': item, 'score': scores}

        return render(request, 'student/changeinfo.html', content)
